package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.domain.req.MachiningReq;
import com.ruibang.glass.produce.domain.resp.MeasureGraphResp;
import com.ruibang.glass.produce.entity.DbMeasure;
import com.ruibang.glass.produce.mapper.DbMeasureMapper;
import com.ruibang.glass.produce.service.DbMeasureService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.ZoneOffset;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-01-23  14:26
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class DbMeasureServiceImpl extends ServiceImpl<DbMeasureMapper, DbMeasure> implements DbMeasureService {
    @Override
    public IPage<DbMeasure> pageQuery(MachiningReq machiningReq) {
        long end = machiningReq.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long start = machiningReq.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        final IPage<DbMeasure> page = new Page<>(machiningReq.getPageNo(), machiningReq.getPageSize());
        return baseMapper.pageQuery(page, start, end, machiningReq.getGlassId());
    }

    @Override
    public void export(MachiningReq machiningReq, HttpServletResponse response) {
        machiningReq.setPageNo(-1);
        machiningReq.setPageSize(-1);
        IPage<DbMeasure> page = this.pageQuery(machiningReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<DbMeasure> dbMeasureList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), DbMeasure.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("短边研磨电流值数据").doWrite(dbMeasureList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public MeasureGraphResp measureGraph(MachiningReq machiningReq) {
        long end = machiningReq.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long start = machiningReq.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        List<DbMeasure> dbMeasureList = baseMapper.measureGraph(start,end);

        if (CollectionUtils.isNotEmpty(dbMeasureList)) {
            MeasureGraphResp measureGraphResp = new MeasureGraphResp();
            //采集时间
            List<String> collectTime = dbMeasureList.stream().map(DbMeasure::getDateTime).collect(Collectors.toList());
            measureGraphResp.setCollectTime(collectTime);
            //左上（切割面）
            List<String> qgmZs = dbMeasureList.stream().map(DbMeasure::getCymJgqBlclZs).collect(Collectors.toList());
            measureGraphResp.setQgmZs(qgmZs);
            //左下（切割面）
            List<String> qgmZx = dbMeasureList.stream().map(DbMeasure::getCymJgqBlclZx).collect(Collectors.toList());
            measureGraphResp.setQgmZx(qgmZx);
            //右上（切割面）
            List<String> qgmYs = dbMeasureList.stream().map(DbMeasure::getCymJgqBlclYs).collect(Collectors.toList());
            measureGraphResp.setQgmYs(qgmYs);
            //右下（切割面）
            List<String> qgmYx = dbMeasureList.stream().map(DbMeasure::getCymJgqBlclYx).collect(Collectors.toList());
            measureGraphResp.setQgmYx(qgmYx);

            //左上（研磨面）
            List<String> ymmZs = dbMeasureList.stream().map(DbMeasure::getCymJghBlclZs).collect(Collectors.toList());
            measureGraphResp.setYmmZs(ymmZs);
            //左下（研磨面）
            List<String> ymmZx = dbMeasureList.stream().map(DbMeasure::getCymJghBlclZx).collect(Collectors.toList());
            measureGraphResp.setYmmZx(ymmZx);
            //右上（研磨面）
            List<String> ymmYs = dbMeasureList.stream().map(DbMeasure::getCymJghBlclYs).collect(Collectors.toList());
            measureGraphResp.setYmmYs(ymmYs);
            //右下（研磨面）
            List<String> ymmYx = dbMeasureList.stream().map(DbMeasure::getCymJghBlclYx).collect(Collectors.toList());
            measureGraphResp.setYmmYx(ymmYx);


            //左上（研磨量）
            List<String> ymlZs = dbMeasureList.stream().map(DbMeasure::getVZs).collect(Collectors.toList());
            measureGraphResp.setYmlZs(ymlZs);
            //左下（研磨量）
            List<String> ymlZx = dbMeasureList.stream().map(DbMeasure::getVZx).collect(Collectors.toList());
            measureGraphResp.setYmlZx(ymlZx);
            //右上（研磨量）
            List<String> ymlYs = dbMeasureList.stream().map(DbMeasure::getVYs).collect(Collectors.toList());
            measureGraphResp.setYmlYs(ymlYs);
            //右下（研磨量）
            List<String> ymlYx = dbMeasureList.stream().map(DbMeasure::getVYx).collect(Collectors.toList());
            measureGraphResp.setYmlYx(ymlYx);

            return measureGraphResp;
        }
        return null;
    }

}
